package cmd

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"

	"itam/internal/controller"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "IT资产管理",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Middleware(MiddlewareCORS)
				group.Bind(
					controller.Env,
					controller.Md,
					controller.Pm,
					controller.Vm,
				)
			})
			s.Run()
			return nil
		},
	}
)

// MiddlewareCORS allow across origin
func MiddlewareCORS(r *ghttp.Request) {
	corsOptions := r.Response.DefaultCORSOptions()
	//corsOptions.AllowDomain = []string{"*"}
	corsOptions.AllowOrigin = "*"
	corsOptions.AllowCredentials = "false"
	corsOptions.AllowMethods = "POST, GET, OPTIONS, PUT, DELETE"
	corsOptions.AllowHeaders = "Content-Type, Access-Control-Allow-Methods, Access-Control-Request-Headers"
	r.Response.CORS(corsOptions)
	r.Middleware.Next()
}
